home *** CD-ROM | disk | FTP | other *** search
- RCS_ID_C="$Id: autoinitd.c,v 1.6 1993/10/18 05:59:52 jraja Exp $";
- /*
- * SAS C Autoinitialization Functions for Daemons
- *
- * Author: ppessi <Pekka.Pessi@hut.fi>
- *
- * Copyright © 1993 AmiTCP/IP Group, <amitcp-group@hut.fi>
- * Helsinki University of Technology, Finland.
- * All rights reserved.
- *
- * Created : Mon May 24 19:30:06 1993 ppessi
- * Last modified: Mon Oct 18 07:59:28 1993 jraja
- */
-
- /****** inetdlib.doc/autoinitd **********************************************
- *
- * NAME
- * autoinitd - SAS C Autoinitialization Functions for Daemons
- *
- * SYNOPSIS
- * void _STIopenSockets(void);
- * void _STDcloseSockets(void);
- * long server_socket;
- *
- * DESCRIPTION
- * These are SASC autoinitialization functions for internet daemons
- * started by inetd, Internet super-server. Upon startup, the server
- * socket is obtained with ObtainSocket() library call. If successful,
- * the socket id is stored to the global variable server_socket. If the
- * socket is not obtainable, the server_socket contains value -1.
- * If the server_socket is not valid, the server may try to accept() a
- * new connection and act as a stand-alone server.
- *
- * RESULT
- * server_socket - positive socket id for success or -1 for failure.
- *
- * NOTES
- * _STIopenSockets() also checks that the system version is at
- * least 37. It puts up a requester if the bsdsocket.library
- * is not found or is of wrong version.
- *
- * The autoinitialization and autotermination functions are
- * features specific to the SAS C6. However, these functions
- * can be used with other (ANSI) C compilers, too. Example
- * follows:
- *
- * \* at start of main() *\
- *
- * atexit(_STDcloseSockets);
- * _STDopenSockets();
- *
- * AUTHOR
- * Jarno Rajahalme, Pekka Pessi,
- * the AmiTCP/IP Group <amitcp-group@hut.fi>,
- * Helsinki University of Technology, Finland.
- *
- * SEE ALSO
- * serveraccept(), netutil/inetd
- *
- *****************************************************************************
- *
- */
-
- #include <exec/types.h>
- #include <exec/libraries.h>
- #include <dos/dosextens.h>
-
- #include <intuition/intuition.h>
-
- #include <proto/socket.h>
- #include <proto/exec.h>
- #include <proto/intuition.h>
-
- #include <stdlib.h>
-
- #include <inetd.h>
-
- struct Library *SocketBase = 0L;
-
- int errno = 0;
-
- long server_socket = -1;
-
- #ifndef SOCKETNAME
- #define SOCKETNAME "bsdsocket.library"
- #endif
-
- #define SOCKETVERSION 2 /* minimum bsdsocket version to use */
-
- extern STRPTR _ProgramName; /* SAS startup module defines this :-) */
-
- void __stdargs
- _STIopenSockets(void)
- {
- struct Process *me = (struct Process *)FindTask(0L);
- struct DaemonMessage *dm = (struct DaemonMessage *)me->pr_ExitData;
- struct Library *IntuitionBase;
- STRPTR errorStr;
-
- /*
- * Check OS version
- */
- if ((*(struct Library **)4)->lib_Version < 37)
- exit(20);
-
- /*
- * Open bsdsocket.library
- */
- if ((SocketBase = OpenLibrary(SOCKETNAME, SOCKETVERSION)) != NULL) {
- /*
- * Succesfull. Now tell bsdsocket.library the address of our errno
- */
- SetErrnoPtr(&errno, sizeof(errno));
-
- if (dm && server_socket == -1) {
- server_socket = ObtainSocket(dm->dm_Id, dm->dm_Family, dm->dm_Type, 0);
- if (server_socket == -1)
- exit(DERR_OBTAIN);
- if (server_socket != 0)
- Dup2Socket(server_socket, 0);
- if (server_socket != 1)
- Dup2Socket(server_socket, 1);
- if (server_socket != 2)
- Dup2Socket(server_socket, 2);
- }
- return;
- }
- else
- errorStr = "AmiTCP/IP version 2 or later must be started first.";
-
- IntuitionBase = OpenLibrary("intuition.library", 36);
-
- if (IntuitionBase != NULL) {
- struct EasyStruct libraryES;
-
- libraryES.es_StructSize = sizeof(libraryES);
- libraryES.es_Flags = 0;
- libraryES.es_Title = _ProgramName;
- libraryES.es_TextFormat = errorStr;
- libraryES.es_GadgetFormat = "Exit %s";
-
- EasyRequestArgs(NULL, &libraryES, NULL, (APTR)&_ProgramName);
-
- CloseLibrary(IntuitionBase);
- }
- exit(DERR_LIB);
- }
-
- void __stdargs
- _STDcloseSockets(void)
- {
- if (SocketBase) {
- CloseLibrary(SocketBase);
- SocketBase = NULL;
- }
- }
-